
<?php 
function install_db(){
	global $wpdb;
	
	$q_arr = array(
	
"CREATE TABLE IF NOT EXISTS `timetable_client` (
  `idclient` int(11) NOT NULL AUTO_INCREMENT,
  `client_name` varchar(250) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `client_contact` text NOT NULL,
  PRIMARY KEY (`idclient`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;",


"CREATE TABLE IF NOT EXISTS `timetable_clientcourse` (
  `idclientcourse` int(11) NOT NULL AUTO_INCREMENT,
  `course_id` int(11) DEFAULT NULL,
  `paystatus_id` int(11) DEFAULT '1',
  `teacher_id` int(11) DEFAULT NULL,
  `clientcourse_lessontotal` int(10) DEFAULT NULL,
  `clientcourse_count` int(11) NOT NULL DEFAULT '0',
  `coursestatus_id` int(11) DEFAULT NULL,
  `client_id` int(11) DEFAULT NULL,
  `clientcourse_begin` date DEFAULT NULL,
  `clientcourse_end` date DEFAULT NULL,
  `form_id` int(11) NOT NULL DEFAULT '1',
  `clientcourse_adress` text NOT NULL,
  `clientcourse_note` text NOT NULL,
  `clientcourse_paid` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`idclientcourse`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",

"CREATE TABLE IF NOT EXISTS `timetable_mails` (
  `idmail` int(11) NOT NULL AUTO_INCREMENT,
  `sender_userid` int(11) NOT NULL,
  `recepient_userid` int(11) NOT NULL,
  `mail_title` varchar(255) DEFAULT '',  
  `mail_body` text DEFAULT '',
  `mail_new` INT NOT NULL DEFAULT '1',
  `mail_deleted` INT NOT NULL DEFAULT '0',
  PRIMARY KEY (`idmail`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",

"CREATE TABLE IF NOT EXISTS `timetable_course` (
  `idcourse` int(11) NOT NULL AUTO_INCREMENT,
  `course_name` varchar(250) DEFAULT NULL,
  `course_basecost` int(11) DEFAULT '0',
  `course_totalcount` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`idcourse`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",


"INSERT INTO `timetable_course` (`idcourse`, `course_name`, `course_basecost`, `course_totalcount`) VALUES
(1, 'AutoCAD', 7950, 5),
(2, 'ArchiCAD', 7950, 5),
(3, 'Photoshop', 7950, 5),
(4, 'CorelDRAW', 7950, 5),
(5, '3DsMAX', 7950, 5),
(6, 'SketchUp', 7950, 5),
(7, 'База ПК', 4950, 5);",


"CREATE TABLE IF NOT EXISTS `timetable_coursestatus` (
  `idcoursestatus` int(11) NOT NULL AUTO_INCREMENT,
  `coursestatus_name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`idcoursestatus`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;",


"INSERT INTO `timetable_coursestatus` (`idcoursestatus`, `coursestatus_name`) VALUES
(1, 'Записан'),
(2, 'Не завершен'),
(3, 'Завершен');",


"CREATE TABLE IF NOT EXISTS `timetable_form` (
  `idform` int(11) NOT NULL AUTO_INCREMENT,
  `form_name` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`idform`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",


"INSERT INTO `timetable_form` (`idform`, `form_name`) VALUES
(1, 'Индивидуальная'),
(2, 'Групповая'),
(3, 'Дистанционная'),
(4, 'Корпоративная');",


"CREATE TABLE IF NOT EXISTS `timetable_paystatus` (
  `idpaystatus` int(11) NOT NULL DEFAULT '0',
  `paystatus_name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`idpaystatus`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;",


"INSERT INTO `timetable_paystatus` (`idpaystatus`, `paystatus_name`) VALUES
(1, 'Не оплачен'),
(2, 'Частичная оплата'),
(3, 'Оплачен'),
(4, 'Аванс');",


"CREATE TABLE IF NOT EXISTS `timetable_role` (
  `idrole` int(11) NOT NULL DEFAULT '0',
  `role_name` varchar(30) DEFAULT NULL,
  `role_status` int(11) NOT NULL,
  PRIMARY KEY (`idrole`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;",


"INSERT INTO `timetable_role` (`idrole`, `role_name`, `role_status`) VALUES
(1, 'Admin', 1),
(2, 'Teacher', 3),
(3, 'Client', 5);",


"CREATE TABLE IF NOT EXISTS `timetable_teacher` (
  `idteacher` int(11) NOT NULL AUTO_INCREMENT,
  `teacher_name` varchar(250) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  `teacher_account` text NOT NULL,
  PRIMARY KEY (`idteacher`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",


"CREATE TABLE IF NOT EXISTS `timetable_teachercourse` (
  `idteachercourse` int(11) NOT NULL AUTO_INCREMENT,
  `teacher_id` int(11) DEFAULT NULL,
  `course_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`idteachercourse`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",


"CREATE TABLE IF NOT EXISTS `timetable_user` (
  `iduser` int(11) NOT NULL AUTO_INCREMENT,
  `wp_user_id` int(11) DEFAULT NULL,
  `role_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`iduser`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;",

"CREATE TABLE IF NOT EXISTS `timetable_order_new` (
    `idorder` int(11) NOT NULL AUTO_INCREMENT,
    `order_status` int(11) NOT NULL DEFAULT '1',
    `order_number` varchar(64) NOT NULL,
    `order_amount` decimal(10,2) NOT NULL,
    `user_id` int(11) NOT NULL,
    `clientcourse_id` int(11) NOT NULL,
    PRIMARY KEY (`idorder`),
    KEY `order_number` (`order_number`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;",

 "CREATE TABLE IF NOT EXISTS `timetable_payment` (
    `idpayment` int(11) NOT NULL AUTO_INCREMENT,
    `user_id` int(11) NOT NULL,
    `clientcourse_id` int(11) NOT NULL,
    `payment_amount` decimal(10,2) NOT NULL,
    `order_id` int(11) NOT NULL,
    `payment_status` int(11) NOT NULL,
    `status` varchar(7) NOT NULL,
    `od_payment_id` int(30) NOT NULL,
    PRIMARY KEY (`idpayment`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;",


 "CREATE TABLE  IF NOT EXISTS `timetable_userinfo` ( 
    `iduserinfo` INT NOT NULL AUTO_INCREMENT , 
    `user_id` INT NOT NULL , 
    `userinfo_sex` TINYINT NOT NULL DEFAULT '0' , 
    `userinfo_birthday` DATE NOT NULL , 
    `userinfo_phone` INT NOT NULL , 
    `userinfo_adress` TEXT NOT NULL,
    PRIMARY KEY (`iduserinfo`),
    KEY `user_id` (`user_id`)
  )ENGINE = MyISAM DEFAULT CHARSET=utf8;"

);


  foreach ($q_arr as $q ) {
    $wpdb->query($q);
  }

  // ALTER PATH  

  $cur_db_q = "SELECT DATABASE() db";
  $res = $wpdb->get_row($cur_db_q, ARRAY_A);
  $cur_db  =   $res['db'];


  //teacher_course
  
  $fields_teacher_q = "SELECT COLUMN_NAME
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = 'timetable_teachercourse'
  AND table_schema = '${$cur_db}';";

  $res = $wpdb->get_results($fields_teacher_q,ARRAY_A);
  $alter_need = true;

  foreach ($res as $row) {
      if($row["COLUMN_NAME"]  ==  "teachercourse_main"){
        $alter_need = false;
      }
  }

  if($alter_need == true){
    $alterq  = "ALTER TABLE `timetable_teachercourse` ADD `teachercourse_main` TINYINT NOT NULL DEFAULT '1';";
    $wpdb->query($alterq);
  }



}
?>